// pages/wodeqian_m/wodeqian_m.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
   page:1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var qian_id1 = options.qian_id1;
    app.globalData.qian_id1 = qian_id1
    app.globalData.qy_txid = options.qy_id
    app.globalData.img_id1 = ""
    app.globalData.img_id2 = ""
    console.log(app.globalData.corpid)
    wx.request({
      url: app.globalData.url_l+'api/member/gongzhang_list',
      method: 'post',
      data: {
        corpid: app.globalData.corpid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        that.setData({
          gz_sj:res.data
        })
      }
    })
  },
  xz:function(e){
    console.log(1)
    var that = this;
    
    var mr_id = e.currentTarget.dataset.id; 
    var mr_url1 = e.currentTarget.dataset.url;
    that.data.page++;
    if(that.data.page==2){
      if(app.globalData.qy_txid==1){
        wx.showToast({
          title: '选择成功',
          icon: 'none',
          duration: 1000,
          success:function(){
            console.log('haha');
            setTimeout(function () {
              wx.navigateBack()
              that.data.page=1
            }, 1000) //延迟时间
          }
        })
        app.globalData.img_id1=1,
        app.globalData.gz_xz = mr_url1
      }else{
        if(app.globalData.qian_id1==1){
          app.globalData.img_id2 = 1
          app.globalData.mr_url1 = mr_url1
          wx.navigateBack()
          that.data.page=1
        }else{
          that.data.page=1
        }
      }
    }else{
      that.data.page=1
    }
    
  
   
   
  },
  shan_c:function(e){
    var that = this;
    var shan_c = e.currentTarget.dataset.id; 
    wx.showModal({
      title: '温馨提示',
      content: '是否删除该公章',
      success (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.url_l+'api/member/gongzhang_de',
            method: 'post',
            data: {
              id: shan_c,
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              console.log(res)
              if(res.data.code==1){
                wx.showToast({
                  title: '删除成功！',
                  icon: 'none',
                  duration: 1000,
                  success:function(){
                    setTimeout(function () {
                      //要延时执行的代码
                      wx.request({
                        url: app.globalData.url_l+'api/member/gongzhang_list',
                        method: 'post',
                        data: {
                          corpid: app.globalData.corpid
                        },
                        header: {
                          'content-type': 'application/x-www-form-urlencoded'
                        },
                        success: function (res) {
                          console.log(res)
                          that.setData({
                            gz_sj:res.data
                          })
                        }
                      })
                    }, 500) //延迟时间
                  }
                })
               
              }else{
                wx.showToast({
                  title: '删除失败！',
                  icon: 'none',
                  duration: 500
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  tianjia_qm:function(e){
    wx.navigateTo({
      url: '../qianming/qianming',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  previewImage(e) {
    var that = this;
    var cur = e.target.dataset.src; //获取本地一张图片链接
    console.log(cur)
    wx.previewImage({
      current: cur, //字符串，默认显示urls的第一张
      urls: [cur] // 数组，需要预览的图片链接列表
    })

  },
  chooseimage3: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9  
      // 可以指定是原图还是压缩图，默认二者都有  
      sizeType: ['original', 'compressed'],
      // 可以指定来源是相册还是相机，默认二者都有
      sourceType: ['album', 'camera'],
      // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片   
      success: function (res) {
        //前台显示
        that.setData({
          tempFilePaths3: res.tempFilePaths
        })

        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths3 = res.tempFilePaths
        wx.showLoading({
          title: '上传中...',
          mask: true,
        })
        const uploadTask =  wx.uploadFile({
          url: 'https://shy.xihenet.net/api/index/order_img',
          filePath: tempFilePaths3[0],
          name: 'file',
          header: {
            "content-type": "multipart/form-data",
            "content-type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            //打印
          
            app.globalData.file5 = res.data
            var str = app.globalData.file5;
            str.replace(/^\"|\"$/g, '');
            app.globalData.file5 = str.replace(/^\"|\"$/g, '');
            console.log(app.globalData.file5)
            wx.request({
              url: app.globalData.url_l+'api/member/gongzhang_tian',
              method: 'post',
              data: {
                gz_img: app.globalData.file5,
                corpid:app.globalData.corpid
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                console.log(res)
                if(res.data.code==1){
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 1000,
                    success:function(){
                      setTimeout(function () {
                        //要延时执行的代码
                        wx.request({
                          url: app.globalData.url_l+'api/member/gongzhang_list',
                          method: 'post',
                          data: {
                            corpid: app.globalData.corpid
                          },
                          header: {
                            'content-type': 'application/x-www-form-urlencoded'
                          },
                          success: function (res) {
                            console.log(res)
                            uploadTask.onProgressUpdate((res) => {
                              console.log('上传进度', res.progress)
                              if(res.progress=='100'){
                                setTimeout(function () {
                                  wx.hideLoading()   //隐藏loading
                                }, 1000)
                              }
                              console.log('已经上传的数据长度', res.totalBytesSent)
                              console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
                            })
                            that.setData({
                              gz_sj:res.data
                            })
                          }
                        })
                      }, 1000) //延迟时间
                    }
                  })
                }else{
                  wx.showToast({
                    title:res.data.msg,
                    icon: 'none',
                    duration: 1000
                  })
                }
              }
            })
            // that.setData({
            //   tempFilePaths3: "https://shy.xihenet.net/uploads/image/"+app.globalData.file5,
            //   xs:"display:block"
            // })
          }, fail: function (err) {
          }
        })
      }
    })
  },
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     var that=this;
     wx.request({
      url: app.globalData.url_l+'api/member/gongzhang_list',
      method: 'post',
      data: {
        corpid: app.globalData.corpid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        that.setData({
          gz_sj:res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})